package com.jiehoo.elove.robot;



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import rath.msnm.SwitchboardSession;
import rath.msnm.UserStatus;
import rath.msnm.entity.MsnFriend;

public class FriendListRecorder {

  private static Log log = LogFactory.getLog(FriendListRecorder.class);

  private List<MsnFriend> onlineFriends = new ArrayList<MsnFriend>();

  private Set<String> busyFriends = new HashSet<String>();

  private Set<String> askingFriends = new HashSet<String>();

  public FriendListRecorder() {

  }

  public boolean friendOnline(MsnFriend friend) {
    if (!onlineFriends.contains(friend)) {
      onlineFriends.add(friend);
      return true;
    } else {
      return false;
    }
  }

  public void friendOffline(String friend) {
    MsnFriend f = getByLoginName(friend);
    if (f != null) {
      onlineFriends.remove(f);
    } else {
      log.error("Can not find " + friend + ", it's should be a bug!");
    }
  }

  protected MsnFriend getByLoginName(String friend) {
    for (int i = 0; i < onlineFriends.size(); i++) {
      MsnFriend f = (MsnFriend) onlineFriends.get(i);
      if (friend.equals(f.getLoginName())) {
        return f;
      }
    }
    return null;
  }

  public String getFreeFriend(String loginname) {
    for (int i = 0; i < onlineFriends.size(); i++) {
      MsnFriend friend = (MsnFriend) onlineFriends.get(i);
      String friendLoginName = friend.getLoginName();
      if (isOnline(friend)) {
        if (!loginname.equals(friendLoginName)
            && !busyFriends.contains(friendLoginName)) {
          busyFriends.add(friendLoginName);
          busyFriends.add(loginname);
          return friendLoginName;
        }
      }
    }
    return null;
  }

  public boolean isOnline(MsnFriend friend) {
    String status = friend.getStatus();
    if (status.equals(UserStatus.ONLINE) || status.equals(UserStatus.IDLE)) {
      return true;
    } else {
      return false;
    }
  }

  public boolean hasFreeFriend() {
    log.debug("Current online friend count:" + onlineFriends.size());
    return onlineFriends.size() - busyFriends.size() > 1 ? true : false;
  }

  public int sessionEnd(SwitchboardSession session) {
    int count = 0;
    Iterator friends = session.getMsnFriends().iterator();
    while (friends.hasNext()) {
      MsnFriend friend = (MsnFriend) friends.next();
      if (busyFriends.remove(friend.getLoginName())) {
        count++;
      }
    }
    return count;
  }

  public boolean friendPartSession(SwitchboardSession session, MsnFriend friend) {
    String friendLoginName = friend.getLoginName();
    if (askingFriends.contains(friendLoginName)) {
      try {
        session.sendMessage(RobotServer.exit);
      }
      catch (IOException e) {
        log.error("Exit session error!", e);
      }
      busyFriends.remove(friendLoginName);
      return askingFriends.remove(friendLoginName);
    } else {
      return busyFriends.remove(friend.getLoginName());
    }
  }

  public boolean friendAsking(MsnFriend friend) {
    return askingFriends.add(friend.getLoginName());
  }
}